define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ykjp/information/basisinfo/customerinfo/index' + location.search,
                    add_url: 'ykjp/information/basisinfo/customerinfo/add',
                    edit_url: 'ykjp/information/basisinfo/customerinfo/edit',
                    del_url: 'ykjp/information/basisinfo/customerinfo/del',
                    multi_url: 'ykjp/information/basisinfo/customerinfo/multi',
                    table: 'customer_info',
                }
            });

            var table = $("#table");
            // table.on('post-common-search.bs.table', function (event, table) {
            //     var form = $("form", table.$commonsearch);
            //     $("input[name='city']", form).addClass("selectpage").data("source", "ykjp/information/basisinfo/customerinfo/selectpage").data("primaryKey", "city").data("field", "city").data("orderBy", "id desc");
            //     $("input[name='address']", form).addClass("selectpage").data("source", "ykjp/information/basisinfo/customerinfo/selectpage").data("primaryKey", "address").data("field", "address").data("orderBy", "id desc");
            //     $("input[name='salesman_id']", form).addClass("selectpage").data("source", "auth/admin/selectpage").data("primaryKey", "id").data("field", "salesman_id").data("orderBy", "id desc");
            //     Form.events.cxselect(form);
            //     Form.events.selectpage(form);
            // });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'code', title: __('Code'), visible: false, operate: false},
                        {field: 'name', title: __('Name')},
                        {field: 'level', title: __('Level'), operate: false},
                        {field: 'contact', title: __('Contact')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'telephone', title: __('Telephone')},
                        {field: 'fax', title: __('Fax')},
                        {field: 'salesman_id', title: __('Salesman_id'), formatter: Table.api.formatter.normal, visible: false, addClass: "selectpage", extend: "data-source='ykjp/information/basisinfo/customerinfo/getsalesman' data-field='username'"},
                        {field: 'city', title: __('City'), visible: false, addClass: "selectpage", extend: "data-source='ykjp/information/basisinfo/customerinfo/selectpage' data-field='city'"},
                        {field: 'address', title: __('Address'), visible: false, addClass: "selectpage", extend: "data-source='ykjp/information/basisinfo/customerinfo/selectpage' data-field='address'"},
                        {field: 'bank', title: __('Bank'), visible: false, operate: false},
                        {field: 'banknumber', title: __('Banknumber'), visible: false, operate: false},
                        {field: 'period', title: __('Period'), operate: false},
                        {field: 'idenums', title: __('Idenums'), visible: false, operate: false},
                        {field: 'crecode', title: __('Crecode'), visible: false, operate: false},
                        {field: 'amount', title: __('Amount'), operate: 'BETWEEN', visible: false, operate: false},
                        {field: 'firmid', title: __('Firmid'), visible: false, operate: false},
                        {field: 'status', title: __('Status'), searchList: {"正常": __('正常'), "禁用": __('禁用')}, formatter: Table.api.formatter.status, },
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime, visible: false, operate: false},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime, visible: false, operate: false},
                        {field: 'deletetime', title: __('Deletetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime, visible: false, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'ykjp/information/basisinfo/customerinfo/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'ykjp/information/basisinfo/customerinfo/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'ykjp/information/basisinfo/customerinfo/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});